//
//  LNStateView.m
//  iLiteNote
//
//  Created by xiangism on 15/9/9.
//  Copyright (c) 2015年 LiteNice. All rights reserved.
//

#import "LNStateView.h"

@interface LNStateView()

@property (nonatomic, strong) UILabel *messageLabel;
@property (nonatomic) NSInteger layTime;

@end

@implementation LNStateView

/*
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithMessage:(NSString*)message layTime:(NSInteger)time {
    CGRect frame =  [[UIScreen mainScreen]bounds];
    int height = 33;
    frame.origin.y = 65;
    frame.size.height = height;
    
    self = [super initWithFrame:frame];
    
    if (self) {
        self.layTime = time;
        // self.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        self.backgroundColor = [UIColor grayColor];
        CGRect labelFrame = CGRectMake(0, 0, frame.size.width, height);
        self.messageLabel = [[UILabel alloc]initWithFrame:labelFrame];
        self.messageLabel.backgroundColor = [UIColor colorWithRed:100.0/255 green:220.0/255 blue:100.0/255 alpha:1];
        self.messageLabel.text = message;
        self.messageLabel.textAlignment = NSTextAlignmentCenter;
        self.messageLabel.textColor = [UIColor whiteColor];
        [self addSubview:self.messageLabel];
    }
    return self;
}

- (void)show {
    UIWindow *win = [[UIApplication sharedApplication]keyWindow];
    [win addSubview:self];
    [self fadeIn];
}

//动画显示
- (void)fadeIn {
    
    //如果想控制视图的位置动画就用transform吧,别用bounds,frames
    //如果是从一个地方像抽屉一样出来还是得用bounds
//    int height = 33;
//    CGAffineTransform tran = CGAffineTransformMakeScale(1, 0);
//    self.transform = CGAffineTransformTranslate(tran, 0, -33);
    
    CGAffineTransform tran = CGAffineTransformMakeTranslation(0, -33/2);
    self.transform = CGAffineTransformScale(tran, 1, 0);
    
    [UIView animateWithDuration:0.35 animations:^ {
        self.transform = CGAffineTransformMakeTranslation(0, 0);
        
    } completion:^(BOOL finished) {
        if (finished) {
            double delayInSeconds = self.layTime;
            __block LNStateView* bself = self;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
            
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                [bself fadeOut];
            });
        }
    }];
    
}

//动画退出
- (void)fadeOut {
    [UIView animateWithDuration:0.35 animations:^ {
        self.alpha = 0;
    }completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

@end
